package org.jeecg.modules.app.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.MsgResult;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.config.app.WechatUtil;
import org.jeecg.modules.api.member.entity.Member;
import org.jeecg.modules.app.service.IWeChatService;
import org.jeecg.modules.app.vo.IndexVo;
import org.jeecg.modules.helper.MessageHelp;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.Map;


@Api(tags = "微信api")
@RestController
@RequestMapping("/app")
@Slf4j
public class WeChatController {

    @Resource
    IWeChatService weChatService;

    @Resource
    MessageHelp messageHelp;


    @AutoLog(value = "小程序登陆-手机号登录")
    @ApiOperation(value = "小程序登陆", notes = "小程序登陆")
    @PostMapping(value = "/loginPhone")
    public Result<Map<String, Object>> loginPhone(@RequestBody String req) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("mobile","18788888888");
        log.info("loginPhone:{}",req);
        return Result.OK("请求成功", weChatService.loginPhone(jsonObject.toJSONString()));
    }

    @AutoLog(value = "小程序登陆")
    @ApiOperation(value = "小程序登陆", notes = "小程序登陆")
    @PostMapping(value = "/login")
    public Result<Map<String, Object>> login(@RequestBody String req) {
        log.info("login:{}",req);
        return Result.OK("请求成功", weChatService.getUserInfo(req));
    }


    @AutoLog(value = "小程序退出登陆")
    @ApiOperation(value = "小程序退出登陆", notes = "小程序退出登陆")
    @PostMapping(value = "/logout")
    public Result logout(HttpServletRequest request, HttpServletResponse response) {
        String token = request.getHeader("APP_TOKEN");
        weChatService.logout(token);
        return Result.OK();
    }

    @ApiOperation(value = "获取AccessToken", notes = "获取AccessToken")
    @GetMapping(value = "/getAccessToken")
    public String requestGetAccessToken() {
        return WechatUtil.requestGetAccessToken().toString();
    }
    @AutoLog(value = "发送短信验证码")
    @ApiOperation(value = "发送短信验证码", notes = "发送短信验证码")
    @PostMapping(value = "/sendCode")
    public Result sendCode(@RequestBody String req) {
        log.info("sendCode:{}",req);
        Map<String,Object> params = JSONObject.parseObject(req,Map.class);
        MsgResult result = messageHelp.sendValidCode(params);
        log.info("sendCode:{}",result);
        if(result.getCode()==MsgResult.CODE_SUCCESS){
            return  Result.OK("发送成功",result.getData());
        }
        return  Result.error("发送失败");
    }

    @AutoLog(value = "小程序绑定用户")
    @ApiOperation(value = "小程序绑定用户", notes = "小程序绑定用户")
    @PostMapping(value = "/bind")
    public Result<Map<String, Object>> bind(@RequestBody String req) {
        log.info("bind:{}",req);
        return Result.OK("请求成功", weChatService.bindUserInfo(req));
    }

    @AutoLog(value = "更新头像")
    @ApiOperation(value = "更新头像", notes = "更新头像")
    @PostMapping(value = "/updateAvatar")
    public Result<Member> updateAvatar(@RequestBody String req) {
        log.info("updateAvatar:{}",req);
        return Result.OK("请求成功", weChatService.updateAvatar(req));
    }
    @AutoLog(value = "更新手机号")
    @ApiOperation(value = "更新手机号", notes = "更新手机号")
    @PostMapping(value = "/updatePhone")
    public Result<Member> updatePhone(@RequestBody String req) {
        log.info("updatePhone:{}",req);
        return Result.OK("请求成功", weChatService.updatePhone(req));
    }
}
